package com.baizhi.controller;

import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2019/4/3.
 */
@Controller
@RequestMapping("/Guru")
public class GurnController {
    @Autowired
    private GuruService guruService;



    @RequestMapping("/selectAllGur.do")
    @ResponseBody
    public Map selectAllGuru(int rows,int page,String albumName){
        return guruService.selectAllGuru(rows,page,albumName);
    };
    @RequestMapping("/selectAllGuru2.do")
    @ResponseBody
    public Map selectAllGuru2(){
        return guruService.selectAllGuru2();
    };





    @ResponseBody
    @RequestMapping("/addGuru.do")
    public Map addGuru(HttpServletRequest request, Guru guru, MultipartFile Image){
        if(Image!=null){
            String fileName=Image.getOriginalFilename();
            String st = UUID.randomUUID().toString();
            String newName=st+fileName;
            String realPathNewMethod = request.getServletContext().getRealPath("img/gurnImage");
            try {
                Image.transferTo(new File(realPathNewMethod+"/"+newName));
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            guru.setGuruImage("/img/gurnImage/"+newName);
            return  guruService.addGuru(guru);
        }else {
            guru.setGuruImage("/img/gurnImage/"+"1.jpg");
            return  guruService.addGuru(guru);
        }
    }




    @ResponseBody
    @RequestMapping("/delateGuru.do")
    public Map delateGuru(Integer[] ids){
        return  guruService.delateGuru(ids);
    }



    @ResponseBody
    @RequestMapping("/updateGuru.do")
    public Map updateGuru(HttpServletRequest request, Guru guru, MultipartFile Image){
        System.out.println(Image);
        if(Image!=null){
            System.out.println("111111111111111111");
            String fileName=Image.getOriginalFilename();
            String st = UUID.randomUUID().toString();
            String newName=st+fileName;
            String realPathNewMethod = request.getServletContext().getRealPath("img/gurnImage");
            try {
                Image.transferTo(new File(realPathNewMethod+"/"+newName));
            } catch (IllegalStateException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            guru.setGuruImage("/img/gurnImage/"+newName);
            return  guruService.updateGuru(guru);
        }else {
            System.out.println("222222222222222222222");
            return  guruService.updateGuru(guru);
        }

    }



}
